﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace YQGL.Common
{
    public class AdminBaseController : Controller
    {
        public AdminBaseController()
        { 
        }


        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            string LoginID = OperateSession.get("LoginID");
            if (string.IsNullOrEmpty(LoginID))
            {
                // Response.Redirect(Url.Action("Index", "Index"));

                filterContext.Controller.ViewBag.Error = "未授权";
                filterContext.HttpContext.Response.Write(@" <script>
                    parent.window.location.href='" + Url.Action("Index", "Index") + @"'
                     window.location.href='" + Url.Action("Index", "Index") + @"'
                    </script>");
                filterContext.HttpContext.Response.Flush();
                filterContext.HttpContext.Response.End();

            }
        }

      
    }

    public class EnterpriseBaseController : Controller
    {
        public EnterpriseBaseController()
        {
        }


        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            string LoginID = OperateSession.get("LoginID");
            if (string.IsNullOrEmpty(LoginID))
            {
               // Response.Redirect(Url.Action("Index", "AdminLogin"));

                filterContext.Controller.ViewBag.Error = "未授权";
                filterContext.HttpContext.Response.Write(@" <script>
                    parent.window.location.href='" + Url.Action("Index", "AdminLogin") + @"'
                     window.location.href='" + Url.Action("Index", "AdminLogin") + @"'
                    </script>");
                filterContext.HttpContext.Response.Flush();
                filterContext.HttpContext.Response.End();

            }
        }


    }
    /// <summary>
    /// 菜单是否授权判断
    /// </summary>
    public class AuthorityActionFilter : ActionFilterAttribute
    {
        /// <summary>
        /// 菜单编码
        /// </summary>
        public string MnuName;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mnuName">菜单编码</param>
        public AuthorityActionFilter(string mnuName)
        {
            MnuName = mnuName;
        }
        /// <summary>
        /// 在action method之前执行
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!string.IsNullOrEmpty(OperateSession.get(GlobalConstant.PARAM_LOGINID)))
            {
                bool isAuthority = false;
                if (MnuName != "")
                {
                    //List<Model.PrvFunctList> prvFunctListList = OperateSession.getList<Model.PrvFunctList>(GlobalConstant.PARAM_PRVFUNCTIONLIST);
                    //var prvList = prvFunctListList.Where(s => s.FFunctNo == MnuName);
                    //if (prvList.ToList().Count() > 0)
                    //{
                    //    isAuthority = true;
                    //}
                }

                if (!isAuthority)
                {
                    filterContext.Controller.ViewBag.Error = "未授权";
                    filterContext.HttpContext.Response.Write("未授权");
                    filterContext.HttpContext.Response.Flush();
                    filterContext.HttpContext.Response.End();
                }
            }
        }

    }
}
